/*
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3 
 * of the License, or (at your option) any later version.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3, 
 * the Appropriate Legal Notices must display the following words on all interactive user interfaces: 
 * "Copyright (C) 2005-2011 SplendidCRM Software, Inc. All rights reserved."
 */

var sREMOTE_SERVER    = '';
var sAUTHENTICATION   = '';
var sUSER_NAME        = '';
var sPASSWORD         = '';
var sUSER_ID          = '';
var sUSER_LANG        = 'en-US';
var sUSER_DATE_FORMAT = 'MM/dd/yyyy';
var sUSER_TIME_FORMAT = 'h:mm tt';
var sFULL_NAME        = '';
var sTEAM_ID          = '';
var sTEAM_NAME        = '';
var bIS_OFFLINE       = false;
var bENABLE_OFFLINE   = false;
var cbNetworkStatusChanged = null;

function GetIsOffline()
{
	return bIS_OFFLINE;
}

function GetEnableOffline()
{
	return bENABLE_OFFLINE;
}

function LoadCredentials()
{
	try
	{
		//alert('LoadCredentials');
		sREMOTE_SERVER  = (localStorage['REMOTE_SERVER' ] !== undefined) ? localStorage['REMOTE_SERVER' ] : '';
		sAUTHENTICATION = (localStorage['AUTHENTICATION'] !== undefined) ? localStorage['AUTHENTICATION'] : '';
		sUSER_NAME      = (localStorage['USER_NAME'     ] !== undefined) ? localStorage['USER_NAME'     ] : '';
		sPASSWORD       = (localStorage['PASSWORD'      ] !== undefined) ? localStorage['PASSWORD'      ] : '';
		if ( sREMOTE_SERVER !== undefined && sREMOTE_SERVER.length > 0 && Right(sREMOTE_SERVER.value, 1) != '/' )
			sREMOTE_SERVER.value += '/';
		try
		{
			if ( sPASSWORD !== undefined && sPASSWORD.length > 0 )
				sPASSWORD = Aes.Ctr.decrypt(sPASSWORD, 'Splendid', 256)
		}
		catch(e)
		{
			sPASSWORD = '';
		}
	}
	catch(e)
	{
		alert('LoadCredentials ' + e.message);
	}
}

function SaveCredentials(sRemoteServer, sAuthentication, sUserName, sPassword)
{
	try
	{
		//alert('SaveCredentials ' + sRemoteServer + ' ' + sAuthentication + ' ' + sUserName + ' ' + sPassword);
		sREMOTE_SERVER  = sRemoteServer  ;
		sAUTHENTICATION = sAuthentication;
		sUSER_NAME      = sUserName      ;
		sPASSWORD       = sPassword      ;
		localStorage['REMOTE_SERVER' ] = sREMOTE_SERVER ;
		localStorage['AUTHENTICATION'] = sAUTHENTICATION;
		localStorage['USER_NAME'     ] = sUSER_NAME     ;
		if ( sPASSWORD !== undefined && sPASSWORD.length > 0 )
			localStorage['PASSWORD'      ] = Aes.Ctr.encrypt(sPASSWORD, 'Splendid', 256);
		else
			localStorage['PASSWORD'      ] = sPASSWORD      ;
		// 08/30/2011 Paul.  Clear the user after changing credentials. 
		sUSER_ID          = '';
		sUSER_LANG        = 'en-US';
		sUSER_DATE_FORMAT = 'MM/dd/yyyy';
		sUSER_TIME_FORMAT = 'h:mm tt';
	}
	catch(e)
	{
		alert('SaveCredentials ' + e.message);
	}
}

function ValidateCredentials()
{
	if ( sREMOTE_SERVER === undefined || sREMOTE_SERVER.length == 0 )
	{
		//alert('ValidateCredentials sREMOTE_SERVER is invalid ' + sREMOTE_SERVER);
		return false;
	}
	if ( sAUTHENTICATION === undefined || (sAUTHENTICATION != 'CRM' && sAUTHENTICATION != 'Basic' && sAUTHENTICATION != 'Windows') )
	{
		//alert('ValidateCredentials sAUTHENTICATION is invalid ' + sAUTHENTICATION);
		return false;
	}
	return true;
}


